package com.company.oa.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.meeting.entity.Meeting;
import com.company.oa.meeting.entity.MeetingAttachment;
import com.company.oa.meeting.mapper.MeetingAttachmentMapper;
import com.company.oa.meeting.mapper.MeetingMapper;
import com.company.oa.meeting.service.MeetingAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

/**
 * 会议附件服务实现类
 */
@Service
public class MeetingAttachmentServiceImpl extends ServiceImpl<MeetingAttachmentMapper, MeetingAttachment> implements MeetingAttachmentService {

    @Value("${file.upload.path:/tmp/uploads}")
    private String uploadPath;

    @Autowired
    private MeetingMapper meetingMapper;

    @Override
    @Transactional
    public Long uploadAttachment(MultipartFile file, Long meetingId, Long uploaderId, String uploaderName) {
        // 检查会议是否存在
        Meeting meeting = meetingMapper.selectById(meetingId);
        if (meeting == null) {
            throw new RuntimeException("会议不存在");
        }
        
        // 获取文件信息
        String originalFilename = file.getOriginalFilename();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        
        // 生成文件路径
        String dateDir = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        String fileName = UUID.randomUUID().toString().replace("-", "") + fileExtension;
        String relativePath = "meeting/" + dateDir + "/" + fileName;
        String fullPath = uploadPath + "/" + relativePath;
        
        // 创建目录
        File dir = new File(fullPath.substring(0, fullPath.lastIndexOf("/")));
        if (!dir.exists()) {
            dir.mkdirs();
        }
        
        // 保存文件
        try {
            file.transferTo(new File(fullPath));
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败", e);
        }
        
        // 保存附件信息
        MeetingAttachment attachment = new MeetingAttachment();
        attachment.setMeetingId(meetingId);
        attachment.setName(originalFilename);
        attachment.setPath(relativePath);
        attachment.setSize(file.getSize());
        attachment.setType(fileExtension.substring(1));
        attachment.setUploaderId(uploaderId);
        attachment.setUploaderName(uploaderName);
        attachment.setUploadTime(LocalDateTime.now());
        attachment.setIsDeleted(0);
        
        save(attachment);
        
        return attachment.getId();
    }

    @Override
    public List<MeetingAttachment> getByMeetingId(Long meetingId) {
        LambdaQueryWrapper<MeetingAttachment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MeetingAttachment::getMeetingId, meetingId)
               .eq(MeetingAttachment::getIsDeleted, 0)
               .orderByDesc(MeetingAttachment::getUploadTime);
        
        return list(wrapper);
    }

    @Override
    @Transactional
    public boolean deleteAttachment(Long id) {
        MeetingAttachment attachment = getById(id);
        if (attachment == null) {
            throw new RuntimeException("附件不存在");
        }
        
        // 逻辑删除
        attachment.setIsDeleted(1);
        
        return updateById(attachment);
    }
} 